package com.yylending.las.personal;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.AnalyticsConfig;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.BuildConfig;
import com.yylending.las.Global;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.personal.version.UpdateService;
import com.yylending.las.personal.version.VersionPresenter;
import com.yylending.las.splash.GuideActivity;
import com.yylending.las.webview.WebViewActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by HenryZhang on 2016/11/3.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class AboutUsActivity extends BaseActivity implements VersionPresenter.View, View.OnClickListener {
	private final static String TAG = "AboutUsActivity";

	private final static int LOADER_ID = 1;

	@BindView(R.id.about_us_icon)
	ImageView mIcon;
	@BindView(R.id.about_us_name)
	TextView mName;
	@BindView(R.id.about_us_version)
	TextView mVersion;
	@BindView(R.id.about_us_company)
	RelativeLayout mCompany;
	@BindView(R.id.about_us_function)
	RelativeLayout mFunction;
	@BindView(R.id.about_us_upgrade)
	RelativeLayout mUpgrade;

	private VersionPresenter mPresenter;
	private UpdateService mUpdateService;

	private static final int CLICK_TIMES = 8;
	private static final int EACH_CLICK_INTERVAL = 200;
	// 需要监听CLICK_TIMES次点击事件
	private long[] mHints = new long[CLICK_TIMES];

	public static void start(Activity aty) {
		Intent i = new Intent(aty, AboutUsActivity.class);
		aty.startActivity(i);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		if (null != mUpdateService) {
			mUpdateService.closeDialog();
		}
		getSupportLoaderManager().destroyLoader(LOADER_ID);
		super.onDestroy();
	}

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about_us);
		ButterKnife.bind(this);
		setTitle(R.string.title_activity_aboutAs);
		mVersion.setText(getVersionName(this));
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	public static String getVersionName(Context ctx) {
		String versionName;
		try {
			PackageManager pm = ctx.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), 0);
			versionName = "v" + pi.versionName;
			if (versionName.length() < 2) {
				return "v1.0.0";
			}
		} catch (Exception e) {
			CLog.e(TAG, "exception " + e.getMessage());
			return "v1.0.0";
		}
		return versionName;
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		switch (id) {
			case LOADER_ID:
				return new PresenterLoader<>(this, new VersionPresenter.Generator());
			default:
				return null;
		}
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		switch (loader.getId()) {
			case LOADER_ID:
				mPresenter = (VersionPresenter) data;
				mPresenter.attach(this);
				break;
		}
	}

	@Override
	public void hasNewVersion(boolean forceUpdate, String url, String content, String title) {
		// TODO: 2017/5/3  升级弹框及处理
		try {//todo 临时解决，点击退出应用crash
			mUpdateService = new UpdateService();
			mUpdateService.showUpdateDialog(this, forceUpdate, url, content, title);
		} catch (Exception e) {
			CLog.e(TAG, e.getMessage());
		}
	}

	@Override
	public void isNewestVersion() {
		showToast(R.string.lastest_version);
	}

	@OnClick({R.id.about_us_company, R.id.about_us_function, R.id.about_us_upgrade, R.id.about_us_icon})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.about_us_company:
				StatService.onEvent(this, StatIDSet.EVENT_ID_COMPANY_INTRODUCTION);
				if (!TextUtils.isEmpty(Global.ABOUT_US_URL)) {
					WebViewActivity.start(this, Global.BASE_URL + Global.ABOUT_US_URL, getString(R.string.company_profile), -1);
				}
				break;
			case R.id.about_us_function:
				StatService.onEvent(this, StatIDSet.EVENT_ID_INTRODUCTION_FUNCTION);
				GuideActivity.start(this, GuideActivity.MODE_VIEW);
				break;
			case R.id.about_us_upgrade:
				mPresenter.checkVersion(true);
				break;
			case R.id.about_us_icon:
				resolveBackDoor();
				break;
			default:
				break;
		}
	}

	private void resolveBackDoor() {
		// 将mHints数组内的所有元素左移一个位置
		System.arraycopy(mHints, 1, mHints, 0, mHints.length - 1);
		// 获得当前系统已经启动的时间
		mHints[mHints.length - 1] = SystemClock.uptimeMillis();
		if (SystemClock.uptimeMillis() - mHints[0] <= CLICK_TIMES * EACH_CLICK_INTERVAL) {
			showLongToast(AnalyticsConfig.getChannel(this) + "_" + BuildConfig.VERSION_NAME + "_" + BuildConfig.VERSION_CODE + "_" + BuildConfig.BUILD_TYPE);
		}
	}
}
